package DatabaseManager;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PushTempBox {

	private Connection con;
	private PreparedStatement preparedStmt;
	private ResultSet resultSet;
	
	private String url;
	private String id;
	private String pw;
		
	/** Methods */
	public PushTempBox(){
		con = null;
		preparedStmt = null;
		resultSet = null;
		url = "jdbc:mysql://127.0.0.1:3306/PushTempBox"; //jdbc:mysql://url:port/'DBname'
		id = "root";
		pw = "wlqwndrydbr";
	}
	
	/**
	 * Connect DB
	 */
	public void ConnectDB(){
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(url,id,pw);
			System.out.println("DB ���� Ȯ��");
		}
		catch (ClassNotFoundException cnte) {
			System.out.println("�ش� Ŭ������ ã�� �� ����ϴ�" + cnte.getMessage() );
		}
		catch (SQLException se) {
			System.out.println(se.getMessage());
		}
	}
	
	public void CloseConnectionDB() throws SQLException{
		con.close();
	}
	/**
	 * Store PDU
	 * @param pdu
	 */
	public void StorePduMessage(String pdu, String address, String msgID){
		String sql = null;
		
		try{
			sql = "insert into PushTempBox values (?,?,?)";
			preparedStmt = con.prepareStatement(sql);
			preparedStmt.setString(1, pdu);	//PDU
			preparedStmt.setString(2, address);		//Address
			preparedStmt.setString(3, msgID);	//MSGID
			preparedStmt.executeUpdate();
			System.out.println("pushtemp insert complete");
			
		}catch (SQLException se){
			System.out.println(se.getMessage());
		}
	}
	
	/**
	 * GetPduMessage
	 * @param address
	 * @return
	 * @throws SQLException
	 */

	public String[] GetPduMessage(String address) throws SQLException
	{
		String sql = null;
		List<String> tempResult = new ArrayList<String>();
		String[] result;	
		
		sql = "select PDU from PushTempBox where ReceiverAddress = ?";
		preparedStmt = con.prepareStatement(sql);
		preparedStmt.setString(1, address);	//PDU
		resultSet = preparedStmt.executeQuery();
		System.out.println("get complete");
		
		while(resultSet.next()){
			tempResult.add(resultSet.getString("PDU"));
		}
	
		result = tempResult.toArray(new String[tempResult.size()]);
		
		return result;
	}
	/**
	 * Delete PDU
	 * @param pdu
	 * @throws SQLException 
	 */
	public void DeletePduMessage(String pdu){
		String sql = null;
		
		try{
			sql = "delete from PushTempBox where PDU = ?";
			preparedStmt = con.prepareStatement(sql);
			preparedStmt.setString(1, pdu);
			//preparedStmt.setString(2, pdu[1]);
			
			preparedStmt.executeUpdate();
		}catch(SQLException se){
			System.out.println(se.getMessage());
		}
	}
	
	/**
	 * �ִ��� ����
	 * @param pdu
	 * @return
	 * @throws SQLException
	 */
	
	public boolean IsStoredPdu(String address) throws SQLException{
		String sql = null;
		try{
			sql = "select * from PushTempBox where ReceiverAddress = ?";
			preparedStmt = con.prepareStatement(sql);
			preparedStmt.setString(1, address);
			//preparedStmt.setString(2, address);
			resultSet = preparedStmt.executeQuery();
		}catch(SQLException se){
			System.out.println(se.getMessage());
		}
		
		if(resultSet.next())
		{
			return true;
		}else
			return false;
	}
}
